package com.bar.web.support;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 控制器超类
 * 
 * @author 杨浩泉
 * @version 2016年10月14日
 */
public class SupperController {

	protected Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	protected HttpServletRequest request;

	@Autowired
	protected HttpServletResponse response;

	/** 视图前缀 */
	protected String viewPrefix;

	public SupperController() {
		setViewPrefix(defaultViewPrefix());
	}

	/**
	 * 设置请求成功响应信息
	 * 
	 * @return
	 */
	protected ResponseEntity<Map<String, Object>> setAjaxMapSuccess() {
		return setAjaxMap(HttpStatus.OK, null, null, null);
	}

	protected ResponseEntity<Map<String, Object>> setAjaxMapSuccess(String msg) {
		return setAjaxMap(HttpStatus.OK, msg, null, null);
	}

	protected ResponseEntity<Map<String, Object>> setAjaxMapSuccess(Object data) {
		return setAjaxMap(HttpStatus.OK, null, data, null);
	}

	protected ResponseEntity<Map<String, Object>> setAjaxMapSuccess(String msg, Object data) {
		return setAjaxMap(HttpStatus.OK, msg, data, null);
	}

	protected ResponseEntity<Map<String, Object>> setAjaxMapSuccess(HttpStatus httpStatus, String msg, Object data) {
		return setAjaxMap(HttpStatus.OK, msg, data, null);
	}

	/**
	 * 设置请求失败响应信息
	 * 
	 * @return
	 */
	protected ResponseEntity<Map<String, Object>> setAjaxMapFailed() {
		return setAjaxMap(HttpStatus.INTERNAL_SERVER_ERROR, null, null, null);
	}

	protected ResponseEntity<Map<String, Object>> setAjaxMapFailed(String msg) {
		return setAjaxMap(HttpStatus.INTERNAL_SERVER_ERROR, msg, null, null);
	}

	protected ResponseEntity<Map<String, Object>> setAjaxMapFailed(HttpStatus httpStatus, String msg) {
		return setAjaxMap(httpStatus, msg, null, null);
	}

	protected ResponseEntity<Map<String, Object>> setAjaxMapFailed(HttpStatus httpStatus, String msg, Object data) {
		return setAjaxMap(httpStatus, msg, data, null);
	}

	/**
	 * 设置请求响应信息
	 * 
	 * @param httpStatus
	 * @param msg
	 * @param data
	 * @return
	 */
	protected ResponseEntity<Map<String, Object>> setAjaxMap(HttpStatus httpStatus, String msg, Object data) {
		return setAjaxMap(httpStatus, msg, data, null);
	}

	protected ResponseEntity<Map<String, Object>> setAjaxMap(HttpStatus httpStatus, String msg, Object data, Object attrs) {
		HashMap<String, Object> map = new HashMap<String, Object>();

		if (null != data) {
			map.put("data", data);
		}
		if (null != attrs) {
			map.put("attrs", attrs);
		}

		map.put("httpCode", httpStatus.value());
		map.put("msg", (null != msg && !"".equals(msg.trim()) ? msg : httpStatus.value()));
		map.put("timestamp", System.currentTimeMillis());

		return new ResponseEntity<Map<String, Object>>(map, httpStatus);
	}

	/**
	 * 获取Controller注解上的路径
	 * 
	 * @return
	 */
	protected String defaultViewPrefix() {
		String currentViewPrefix = "";
		RequestMapping requestMapping = AnnotationUtils.findAnnotation(getClass(), RequestMapping.class);
		if (requestMapping != null && requestMapping.value().length > 0) {
			currentViewPrefix = requestMapping.value()[0];
		}
		return currentViewPrefix;
	}

	/**
	 * 设置视图名称
	 * 
	 * @param suffixName
	 * @return
	 */
	public String viewName(String suffixName) {
		if (!suffixName.startsWith("/")) {
			suffixName = "/" + suffixName;
		}
		return getViewPrefix() + suffixName;
	}

	/**
	 * 设置视图前缀
	 * 
	 * @param viewPrefix
	 */
	public void setViewPrefix(String viewPrefix) {
		if (viewPrefix.startsWith("/")) {
			viewPrefix = viewPrefix.substring(1);
		}
		this.viewPrefix = viewPrefix;
	}

	public String getViewPrefix() {
		return viewPrefix;
	}

}
